package com.a2v2.online.store.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.a2v2.online.store.common.logger.contract.ILogger;

@Aspect
public class LoggingAspect {
	
	static ILogger logger = null;
	
	public LoggingAspect(ILogger logger){
		this.logger= logger;
	}
	
	//within(com.po.cscp.http..*)  //execution(* com.expresspay.*(..))
	//TODO right now its for services and dao package in coming change need to cover all required packages.
	@Pointcut("within(com.expresspay.service..*) || within(com.expresspay.dao..*)")
	private void logging() { }
	
	 
	@Before("logging()")
	public void afterCall(JoinPoint jp) {
		 Signature  signature= jp.getSignature();
		 logger.logInfo(signature.getClass(), "Entry in "+signature.getName());
		 logger.logInfo(signature.getClass(), "Argument list passed "+jp.getArgs());
	}

	@AfterReturning("logging()")
	public void finishedCall(JoinPoint jp) {
		 Signature  signature= jp.getSignature();
		 logger.logInfo(signature.getClass(), "Exit in"+signature.getName());
	}
	
	public static void logInfo(Class clazz, String message){
		logger.logInfo(clazz, message);
	}
	public static void logError(Class clazz, String message, Throwable t ){
		logger.logError(clazz, message, t);
	}
	public static void logDebug(Class clazz, String message ){
		logger.logDebug(clazz, message);
	}
}
